<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function get_document_info($handle, $index) {
    $info = array();
    $line = fgets($handle);
    while($line && strcasecmp(rtrim($line), ".I ".$index) != 0) {
        $line = fgets($handle);
    }
    $line = fgets($handle);
    while($line && strcasecmp(rtrim($line), ".T") != 0) {
         $line = fgets($handle);
    }
    $line = fgets($handle);
    $info["title"] = $line;
    while($line && strcasecmp(rtrim($line), ".A") != 0) {
         $line = fgets($handle);
    }
    $line = fgets($handle);
    $info["author"] = $line;
    while($line && strcasecmp(rtrim($line), ".W") != 0) {
         $line = fgets($handle);
    }
    $line = fgets($handle);
    $line2 = fgets($handle);
    $info["body"] = $line."<br />".$line2;
    fseek($handle, 0);
    return $info;
}


function is_stopword($stopwords, $word) {
    // disini di
    foreach ($stopwords as $stopword) {
        if (strcasecmp($word, $stopword) == 0){
            return true;
        }                
    }
    return false;
}

function file_query_to_array_query($file_query) {
    $i = "";

    $title_string = ".T";
    $author_string = ".A";
    $body_string = ".W";
    $index_string = ".I";
    $contex_string = ".N";

    $is_index = false;
    $is_title = false;
    $is_author = false;
    $is_body = false;
    $is_contex = false;

    $file_col = fopen($file_query, "r");

    $temp_query = "";
    $array_query = array();

    if ($file_col) {
        while (!feof($file_col)) {
            if (($temp_string = fgetcsv($file_col, 10000, " ")) !== FALSE) {
                if (strncasecmp($temp_string[0], $body_string, 2) == 0) {
                    //proses indexing di body
                    $temp_query = "";
                    if (($temp_string = fgetcsv($file_col, 10000, " ")) !== FALSE) {
                        while (strncasecmp($temp_string[0], $index_string, 2) != 0 &&
                        strncasecmp($temp_string[0], $title_string, 2) != 0 &&
                        strncasecmp($temp_string[0], $author_string, 2) != 0 &&
                        strncasecmp($temp_string[0], $body_string, 2) != 0 &&
                        strncasecmp($temp_string[0], $contex_string, 2) != 0 && !feof($file_col)) {
                            foreach ($temp_string as $value) {
                                //echo $value;
                                $term = strtolower($value);
                                $temp_query = $temp_query . $term . " ";
                            }
                            $temp_string = fgetcsv($file_col, 10000, " ");
                        }
                        //echo "[" . $temp_query . "]<br/>";
                        $array_query[] = $temp_query;
                    }
                }
            }
        }
    }
    return $array_query;
}



?>
